﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMS_Master.Dto;
using TMS_Master.Entities;
using TMS_Master.Interface;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace TMS_Master.DomainService
{
    public class Materials_CompanyService: ApplicationService, IMaterials_CompanyService
    {
        #region 注入仓储接口
        private readonly IRepository<Materials_Company, Guid> _cargoRepository; //物资入库

        private readonly IRepository<TmsType, Guid> _typeRepository; //分类

        private readonly IRepository<Texture, Guid> _textureRepository; //材质

        private readonly IRepository<Address, Guid> _addrRepository;  //地址

        public Materials_CompanyService(IRepository<Materials_Company, Guid> cargoRepository, IRepository<Address, Guid> addrRepository, IRepository<Texture, Guid> textureRepository, IRepository<TmsType, Guid> typeRepository)
        {
            _cargoRepository = cargoRepository;
            _typeRepository = typeRepository;
            _textureRepository = textureRepository;
            _addrRepository = addrRepository;
        }
        #endregion

        #region 修改
        [HttpPost]
        public async Task<Materials_CompanyDto> EditMater(Materials_CompanyUpdateDto Mc)
        {
            try
            {
                var res = ObjectMapper.Map<Materials_CompanyUpdateDto, Materials_Company>(Mc);
                var result = await _cargoRepository.UpdateAsync(res);
                return ObjectMapper.Map<Materials_Company, Materials_CompanyDto>(result);
            }
            catch (Exception)
            {

                throw;
            }
            
        }

        
        #endregion

        #region 反填
        [HttpGet]
        public async Task<Materials_CompanyDto> FullMater(Guid id)
        {
            try
            {
                var query = await _cargoRepository.GetAsync(id);
                return ObjectMapper.Map<Materials_Company, Materials_CompanyDto>(query);
            }
            catch (Exception)
            {

                throw;
            }
            
        }

        
        #endregion

        #region 删除
        [HttpPost]
        public async Task<int> DelMater(Guid Id)
        {
            try
            {
                await _cargoRepository.DeleteAsync(Id);
                return 1;
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 详情
        [HttpGet]
        public async Task<Materials_CompanyDto> Details(Guid Id)
        {
            try
            {
                var query = await _cargoRepository.GetAsync(Id);
                return ObjectMapper.Map<Materials_Company, Materials_CompanyDto>(query);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 显示
        [HttpGet]
        public async Task<PagedResultDto<MaterialsDto>> GetList(int PageIndex=1, int PageSize=4, string name="", string Typename = "", string addr = "", string payname = "", string person = "")
        {
            try
            {
                var a = await _cargoRepository.GetListAsync();
                var b = await _typeRepository.GetListAsync();
                var c = await _textureRepository.GetListAsync();
                var d = await _addrRepository.GetListAsync();

                var linq = (from w in a
                            join x in b on w.TypeID equals x.Id
                            join y in c on w.TextureID equals y.Id
                            join z in d on w.AddrID equals z.Id
                            select new MaterialsDto
                            {
                                Id = w.Id,
                                CompanyName = w.CompanyName,
                                TypeID = x.Id,
                                TextureID = y.Id,
                                SkuName = w.SkuName,
                                AddrID = z.Id,
                                Number = w.Number,
                                Price = w.Price,
                                PayName = w.PayName,
                                Money = w.Money,
                                Remark = w.Remark,
                                CreateTime = w.CreateTime,
                                Registrar = w.Registrar,
                                Space = z.Space,
                                PartentID = z.PartentID,
                                TextureName = y.TextureName,
                                TypeName = x.TypeName
                            }).ToList();
                if (!string.IsNullOrEmpty(name))
                {
                    linq = linq.Where(s => s.CompanyName.Contains(name)).ToList();
                }
                if (!string.IsNullOrEmpty(Typename))
                {
                    linq = linq.Where(s => s.TextureID.Equals(Typename)).ToList();
                }
                if (!string.IsNullOrEmpty(addr))
                {
                    linq = linq.Where(s => s.AddrID.Equals(name)).ToList();
                }
                if (!string.IsNullOrEmpty(person))
                {
                    linq = linq.Where(s => s.Registrar.Contains(person)).ToList();
                }
                if (!string.IsNullOrEmpty(payname))
                {
                    linq = linq.Where(s => s.PayName.Equals(payname)).ToList();
                }
                //总条数
                int TotalCount = linq.Count;

                var result = linq.Skip((PageIndex - 1) * PageSize).Take(PageSize);

                var res = ObjectMapper.Map<List<MaterialsDto>, List<MaterialsDto>>(result.ToList());

                return new PagedResultDto<MaterialsDto>(TotalCount, res);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 添加
        [HttpPost]
        public async Task<int> CreateMater(Materials_CompanyUpdateDto Mc)
        {
            try
            {
                var query = ObjectMapper.Map<Materials_CompanyUpdateDto, Materials_Company>(Mc);
                await _cargoRepository.InsertAsync(query);
                return 1;
            }
            catch (Exception)
            {

                throw;
            }
            
        }

        
        #endregion
    }
}
